function t(t) {
    if (t && t.__esModule) return t;
    var e = {};
    if (null != t) for (var i in t) Object.prototype.hasOwnProperty.call(t, i) && (e[i] = t[i]);
    return e.default = t, e;
}

function e(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function i(t) {
    wx.showToast({
        title: t,
        image: "/components/authorize-mobile/images/bad-toast.png",
        duration: 3e3
    });
}

function n(t, e) {
    wx.showModal({
        title: t,
        content: e,
        showCancel: !1,
        confirmText: "知道了"
    });
}

function o(t) {
    var e = null;
    return t.mobile ? t.verifycode || (e = "请输入验证码") : e = "请输入手机号", e && i(e), !e;
}

var s = Object.assign || function(t) {
    for (var e = 1; e < arguments.length; e++) {
        var i = arguments[e];
        for (var n in i) Object.prototype.hasOwnProperty.call(i, n) && (t[n] = i[n]);
    }
    return t;
}, a = e(require("../../public/utils/util")), r = e(require("../../public/utils/user")), u = t(require("../../public/utils/request")), c = t(require("../../public/utils/host")), l = e(require("../../public/utils/api"));

Component({
    properties: {
        isShown: {
            value: !1,
            type: Boolean,
            observer: function(t) {
                this.setData({
                    isShown: t
                });
            }
        }
    },
    ready: function() {
        var t = this;
        r.default.getTokens().then(function(e) {
            t.setData({
                tokens: e
            });
        }), this.initEnv();
    },
    data: {
        source: "weAppSite",
        isShown: !1,
        tokens: {},
        useWechat: !0,
        binding: !1,
        mobile: "",
        countDown: 0,
        verifycode: "",
        authinfo: "授权手机，即可同步订单、优惠券等更多信息！！"
    },
    methods: {
        initEnv: function() {
            "wx" !== getApp().globalData.environment && this.setData({
                useWechat: !1,
                authinfo: "验证手机，同步官网信息！！"
            });
        },
        signinByWechat: function(t) {
            var e = this, o = t.detail, a = o.errMsg, r = o.iv, u = o.encryptedData;
            a.includes("getPhoneNumber:fail") ? (i("快速获取失败"), this.setData({
                useWechat: !1
            })) : (this.setData({
                binding: !0
            }), this.bindingWxUser(s({
                iv: r,
                encryptedData: u
            }, this.data.tokens)).then(function() {
                wx.showToast({
                    title: "授权成功"
                }), e.setData({
                    binding: !1
                }), e.triggerEvent("verifysuccess"), getApp().globalData.tokens.isverifymobile = 1;
            }, function(t) {
                n("授权手机失败", t.message), console.log("Error in bindMobileByWechat: ", t), e.setData({
                    useWechat: !1
                });
            }));
        },
        inputMobile: function(t) {
            var e = t.detail.value;
            this.setData({
                mobile: e
            });
        },
        getVerifycode: function() {
            var t = this, e = this.data, n = e.mobile, o = e.source;
            a.default.isMobile(n) ? (this.setData({
                countDown: 60
            }), this.updateCountDown(Date.now()), l.default.users.sendcode({
                mobile: n,
                source: o
            }).then(function(t) {
                if (wx.showToast({
                    title: "验证码已发送"
                }), 0 !== t.code || !t.data.issend) return Promise.reject(t);
            }).catch(function(e) {
                i("短信发送失败"), t.setData({
                    countDown: 0
                });
            })) : i("手机格式错误");
        },
        inputVerifycode: function(t) {
            var e = t.detail.value;
            this.setData({
                verifycode: e
            });
        },
        submit: function() {
            var t = this.data, e = t.mobile, i = t.verifycode, a = t.tokens;
            o({
                mobile: e,
                verifycode: i
            }) && this.bindingWxUser(s({
                mobile: e,
                verifycode: i
            }, a)).then(function() {
                return wx.showToast({
                    title: "授权成功"
                });
            }).catch(function(t) {
                return n("授权手机失败", t.message);
            });
        },
        bindingWxUser: function(t) {
            var e = this;
            return wx.showLoading({
                title: "授权中..",
                mask: !0
            }), new Promise(function(i, n) {
                var o = getApp().globalData, a = o.app, r = o.appid, u = o.environment, l = c.apiPrefix + "/user/bindmobilev3", f = c.apiHost + "/miniappsuite/user/authmobile", d = "wx" === u ? l : f, h = s({}, t, {
                    app: a,
                    appid: r
                });
                e.getLoginCodeOrSessionkey().then(function(t) {
                    Object.assign(h, t), e.requestBindingWxUser(d, h).then(function(t) {
                        e.statisticslogin("bindmobile"), i(t);
                    });
                });
            });
        },
        requestBindingWxUser: function(t, e) {
            var i = this;
            return new Promise(function(n, o) {
                u.post(t, e).then(function(t) {
                    if (0 === t.code) {
                        r.default.updateTokens(t.data), i.setData({
                            isShown: !1
                        });
                        var e = getCurrentPages(), s = e[e.length - 1];
                        s && s.setData({
                            isverifymobile: 1
                        }), n(t.data), i.triggerEvent("verifysuccess");
                    } else o(t);
                    wx.hideLoading();
                });
            });
        },
        getLoginCodeOrSessionkey: function() {
            var t = this;
            return new Promise(function(e, i) {
                var n = getApp().globalData, o = n.tokens.sessionkey, s = "wx" === n.environment, a = s ? wx.checkSession : wx.qy.checkSession, r = s ? wx.login : wx.qy.login;
                a({
                    success: function() {
                        o ? e({
                            sessionkey: o
                        }) : t.badToast("sessionkey为空");
                    },
                    fail: function() {
                        r({
                            success: function(t) {
                                var i = t.code;
                                e({
                                    code: i
                                });
                            },
                            fail: function(e) {
                                t.badToast(e.errMsg);
                            }
                        });
                    }
                });
            });
        },
        updateCountDown: function(t) {
            var e = this, i = Date.now(), n = this.data.countDown;
            n -= (i - t) / 1e3, this.setData({
                countDown: n
            }), n > 0 && setTimeout(function() {
                return e.updateCountDown(i);
            }, 60);
        },
        colseDialog: function() {
            this.setData({
                isShown: !1
            });
        },
        statisticslogin: function(t) {
            var e = getApp().globalData, i = e.appid, n = e.tokens.openid, o = c.apiPrefix + "/user/statisticslogin";
            return u.post(o, {
                appid: i,
                openid: n,
                op: t
            }).then(function(e) {
                console.log(e, "获取用户信息" + t);
            });
        }
    }
});